<!DOCTYPE HTML>
<html lang="zh-CN">

<head>
    <title>登录</title>
    <meta charset="UTF-8">
    <meta name="viewport" content=" initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <script src="https://og6593g2z.qnssl.com/fundebug.0.3.3.min.js" apikey="e65b1e28cd641345cd31ef6dce8ac62f7aaa3ba3420be5a1c4aa23ee3636992d"></script>
    <link href="{$Think.config.RESOURCEURL}wx_assets/css/common.css{$Think.config.RESOURCE_VER}" rel="stylesheet">
    <link href="{$Think.config.RESOURCEURL}wx_assets/personCenter/css/index.css{$Think.config.RESOURCE_VER}" rel="stylesheet">
    <link href="{$Think.config.RESOURCEURL}wx_assets/iconfont/iconfont.css{$Think.config.RESOURCE_VER}" rel="stylesheet">
    <script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/zepto.1.1.3.min.js{$Think.config.RESOURCE_VER}"></script>
    <script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/zepto.alert.js{$Think.config.RESOURCE_VER}"></script>
    <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
    <script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/wx_share.js{$Think.config.RESOURCE_VER}"></script>
    <script src="{$Think.config.RESOURCEURL}wx_assets/js/util.js{$Think.config.RESOURCE_VER}" type="text/javascript"></script>
    <script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/product/cookie.js{$Think.config.RESOURCE_VER}"></script>
</head>

<body id="login">
    <!-- tab菜单 -->
    <if condition="($isUserNameLogin eq 1) AND ($isPhoneLogin eq 1)">
      <ul class="tab-list">
          <li class="active" data-type="normal">账号密码登录</li>
          <li data-type="fast">快捷免密登录</li>
      </ul>
    </if>
    <!-- 普通登录 -->
    <if condition="$isUserNameLogin eq 1">
      <div class="form normal-login">
          <ul>
              <li>
                  <div>
                    <i class="iconfont">&#xe627;</i>
                    <input type="text" placeholder="请输入手机号" class="phone"/>
                  </div>
              </li>
              <li>
                <div>
                  <i class="iconfont">&#xe6b2;</i>
                  <input type="password" placeholder="请输入密码" class="password"/>
                </div>
              </li>
          </ul>
          <div class="login-btn">登录</div>
      </div>
    </if>
    <!-- 快捷登录 -->
    <if condition="$isPhoneLogin eq 1">
      <if condition="$isUserNameLogin neq 1">
        <div class="form fast-login" style="display: block;">
      <else />
        <div class="form fast-login">
      </if>
        <ul>
            <li>
                <div>
                    <i class="iconfont">&#xe627;</i>
                    <input type="text" placeholder="请输入手机号" class="phone"/>
                </div>
            </li>
            <li>
               <div>
                    <i class="iconfont">&#xe64a;</i>
                    <input type="number" placeholder="请输入验证码" class="code"/>
                    <span class="sms-btn">获取验证码</span>
               </div>
            </li>
        </ul>
        <div class="login-btn">登录</div>
      </div>
    </if>
    <div class="link-feild">
        <if condition="$isRegister eq 1">
          <a href="{$Think.config.SERVER_HOST}call.html?customerId={$customerId}&module=personCenter&method=register">注册</a>
        <else />
          <a href="javascript:;"></a>
        </if>
        <a href="{$Think.config.SERVER_HOST}call.html?customerId={$customerId}&module=personCenter&method=forgotPassword" class="link-forget">忘记密码</a>
    </div>
</body>
<script>
    function Page() {
        '{$isUserNameLogin}' == 1 ? this.loginType = 'normal' : this.loginType = 'fast'
    };

    $.extend(Page.prototype, {
        init: function () {
            this.bindEvents();
            console.log($.util.getUa())
            this.initPage();
        },

        // 绑定事件
        bindEvents: function() {
            $('.login-btn').click($.proxy(this.handleLoginBtnClick, this));
            $('.tab-list li').click($.proxy(this.handleTabclick, this));
            $('.sms-btn').click($.proxy(this.handleSendSmsBtnClick, this));
        },

        initPage: function() {
          if ($.util.getParam('code') == 16) {
            $('.tab-list').css('display', 'none');
            $('.normal-login').css('display', 'none');
            $('.fast-login').css('display', 'block');
            this.loginType = 'fast'
          }
        },

        // 点击切换tab菜单
        handleTabclick: function(event) {
            this.loginType = $(event.target).data('type');
            this.loginType === 'normal' ? $('.link-forget').css('display', 'block') : $('.link-forget').css('display', 'none');
            $(event.target).addClass('active').siblings().removeClass('active');
            $('.form').css('display', 'none');
            $('.' + this.loginType + '-login').css('display', 'block');
        },

        // 点击登录按钮
        handleLoginBtnClick: function() {
          if (this.loginType === 'normal') {
            this.normalLogin();
          } else {
            this.fastLogin();
          }
        },

        // 普通登录
        normalLogin: function() {
          var that = this;
          var phone = $('.normal-login .phone').val();
          var password = $('.normal-login .password').val();
          if (!phone) {
            $.util.alert("请输入手机号码！");
            return false;
          }
          if(!(/^1[34578]\d{9}$/.test(phone))){
            $.util.alert("手机号码有误，请重填");
            return false;
          }
          if (!password) {
            $.util.alert("请输入密码！");
            return false;
          }
          var params = {
            phone: phone,
            password: password
          }
          $.ajax({
              type: "get",
              url: '{$Think.config.SERVER_HOST}call.json?customerId={$customerId}',
              dataType: 'json',
              data: {
                 module: 'personCenter',
                 method: 'login',
                 params: JSON.stringify(params)
              },
              success: $.proxy(this.handleLoginSucc, this),
              error: $.proxy(this.handleLoginError, this)
          });
        },

        // 快速登录
        fastLogin: function() {
          var that = this;
          var phone = $('.fast-login .phone').val();
          var code = $('.fast-login .code').val();
          if (!phone) {
            $.util.alert("请输入手机号码！");
            return false;
          }
          if(!(/^1[34578]\d{9}$/.test(phone))){
            $.util.alert("手机号码有误，请重填");
            return false;
          }
          if (!code) {
            $.util.alert("请输入验证码！");
            return false;
          }
          var params = {
            phone: phone,
            code: code
          }
          $.ajax({
              type: "get",
              url: '{$Think.config.SERVER_HOST}call.json?customerId={$customerId}',
              dataType: 'json',
              data: {
                 module: 'personCenter',
                 method: 'phoneLogin',
                 params: JSON.stringify(params)
              },
              success: $.proxy(this.handleLoginSucc, this),
              error: $.proxy(this.handleLoginError, this)
          });
        },

        // 点击发送验证码按钮
        handleSendSmsBtnClick: function() {
          if ($('.sms-btn').hasClass('disabled')) return;
          var phone = $('.fast-login .phone').val();
          if (!phone) {
            $.util.alert("请输入手机号码！");
            return false;
          }
          if(!(/^1[34578]\d{9}$/.test(phone))){
            $.util.alert("手机号码有误，请重填");
            return false;
          }
          var params = {
            phone: phone
          }
          $.ajax({
              type: "get",
              url: '{$Think.config.SERVER_HOST}call.json?customerId={$customerId}',
              dataType: 'json',
              data: {
                module: 'personCenter',
                method: 'verifyPhone',
                params: JSON.stringify(params)
              },
              success: $.proxy(this.handleGteSmsSucc, this)
          });
        },

        // 获取短信成功后执行
        handleGteSmsSucc: function(res) {
          if (+res.code === 0) {
            $('.sms-btn').addClass('disabled');
            var second = 60;
            $('.sms-btn').text('60s后重新获取')
            var timer = setInterval(function(){
              $('.sms-btn').text(--second + 's后重新获取')
              if (second <= 0) {
                clearInterval(timer);
                $('.sms-btn').removeClass('disabled');
                $('.sms-btn').text('获取验证码');
              }
            }, 1000)
          } else {
            $.util.alert(res.msg);
          }
        },

        // 登录成功后执行
        handleLoginSucc: function(res) {
          if (+res.code === 0) {
            setCookie('token', res.data.token);
            // 如果是易小康APP调用原生方法
            $.util.getUa() === 'nativeApp' && android.saveLogin(res.data.token);
            var redirectUrl = $.util.getParam('redirect_url');
            if (redirectUrl) {
              location.replace(decodeURIComponent(redirectUrl));
            } else {
              location.replace('{$Think.config.SERVER_HOST}call.html?customerId={$customerId}&module=personCenter&method=index');
            }
          } else {
            $.util.alert(res.msg);
          }
        },

        // 登录失败
        handleLoginError: function (res) {
            $.util.alert('登录失败，请重试！')
        }
    });

    var page = new Page();
    page.init();
</script>
</html>
